#include "User_mid_LCD.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "User_dev_inc.h"
#define DELAY(X) vTaskDelay(((X)/ portTICK_PERIOD_MS))

#define TAG "User_mid_LCD"

#define LCD_WR_DATA8(x) VSPI_data_queue_data_8bit(x)
#define LCD_WR_REG(x) VSPI_data_queue_REG_8bit(x)

/******************************************************************************
      函数说明：设置起始和结束地址
      入口数据：x1,x2 设置列的起始和结束地址
                y1,y2 设置行的起始和结束地址
      返回值：  无
******************************************************************************/
void LCD_Address_Set(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
	VSPI_data_queue_REG_8bit(0x2a); //列地址设置
	VSPI_data_queue_data_32bit(x1 >> 8, x1, x2 >> 8, x2);
	VSPI_data_queue_REG_8bit(0x2b); //行地址设置
	VSPI_data_queue_data_32bit(y1 >> 8, y1, y2 >> 8, y2);
	VSPI_data_queue_REG_8bit(0x2c); //储存器写
}

/*16bit rgb 565*/
IRAM_ATTR void LCD_Fill(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t *color)
{
	uint32_t siofs = 240 * 2 * 10; // 同时刷多少行240 *2（8bit）*10行
	uint32_t siof = 240 * 1 * 10;  // 因为数组是16位的，但字节指向的地址
	uint32_t size = (x2 + 1 - x1) * (y2 + 1 - y1) * 2;
	LCD_Address_Set(x1, y1, x2, y2);
	uint32_t send_cnt = size / siofs;  // 需要整行刷新多少行
	uint32_t send_cnt2 = size % siofs; // 整行刷不完还剩多少字节
	if (size == 0)
	{
		ESP_LOGE(TAG, "size为0,跳出了");
		return;
	}
	for (int i = 0; i < send_cnt; i++)
	{
		if ((i + 1) == send_cnt && send_cnt2 == 0)
		{
			VSPI_data_queue(&color[i * siof], siof * 16, SPI_WRITE_DATA_OVER);
		}
		else
		{
			VSPI_data_queue(&color[i * siof], siof * 16, SPI_WRITE_DATA);
		}
	}
	if (send_cnt2 != 0)
	{
		VSPI_data_queue(&color[send_cnt * siof], send_cnt2 * 8, SPI_WRITE_DATA_OVER);
	}
}

void LCD_IO_deinit(void)
{
	BLK_OFF();
	DELAY(100);
	LCD_RES_CLR();
	DELAY(100);
	LCD_RES_SET();
	DELAY(100);
}

/*中景圆1.54屏*/
void ZJY_154_LCD_init(void)
{
	LCD_WR_REG(0x11); 
	DELAY(120);

	LCD_WR_REG(0x36);
	LCD_WR_DATA8(0x00);
	LCD_WR_REG(0x3A); 
	LCD_WR_DATA8(0x05);

	LCD_WR_REG(0xB0);
	LCD_WR_DATA8(0x00);
	LCD_WR_DATA8(0x08);

	LCD_WR_REG(0xB2);
	LCD_WR_DATA8(0x0C);
	LCD_WR_DATA8(0x0C);
	LCD_WR_DATA8(0x00);
	LCD_WR_DATA8(0x33);
	LCD_WR_DATA8(0x33); 

	LCD_WR_REG(0xB7); 
	LCD_WR_DATA8(0x35);  

	LCD_WR_REG(0xBB);
	LCD_WR_DATA8(0x19);

	LCD_WR_REG(0xC0);
	LCD_WR_DATA8(0x2C);

	LCD_WR_REG(0xC2);
	LCD_WR_DATA8(0x01);

	LCD_WR_REG(0xC3);
	LCD_WR_DATA8(0x12);   

	LCD_WR_REG(0xC4);
	LCD_WR_DATA8(0x20);  

	LCD_WR_REG(0xC6); 
	LCD_WR_DATA8(0x0F);    

	LCD_WR_REG(0xD0); 
	LCD_WR_DATA8(0xA4);
	LCD_WR_DATA8(0xA1);

	LCD_WR_REG(0xE0);
	LCD_WR_DATA8(0xD0);
	LCD_WR_DATA8(0x04);
	LCD_WR_DATA8(0x0D);
	LCD_WR_DATA8(0x11);
	LCD_WR_DATA8(0x13);
	LCD_WR_DATA8(0x2B);
	LCD_WR_DATA8(0x3F);
	LCD_WR_DATA8(0x54);
	LCD_WR_DATA8(0x4C);
	LCD_WR_DATA8(0x18);
	LCD_WR_DATA8(0x0D);
	LCD_WR_DATA8(0x0B);
	LCD_WR_DATA8(0x1F);
	LCD_WR_DATA8(0x23);

	LCD_WR_REG(0xE1);
	LCD_WR_DATA8(0xD0);
	LCD_WR_DATA8(0x04);
	LCD_WR_DATA8(0x0C);
	LCD_WR_DATA8(0x11);
	LCD_WR_DATA8(0x13);
	LCD_WR_DATA8(0x2C);
	LCD_WR_DATA8(0x3F);
	LCD_WR_DATA8(0x44);
	LCD_WR_DATA8(0x51);
	LCD_WR_DATA8(0x2F);
	LCD_WR_DATA8(0x1F);
	LCD_WR_DATA8(0x1F);
	LCD_WR_DATA8(0x20);
	LCD_WR_DATA8(0x23);

	LCD_WR_REG(0x21); 

	LCD_WR_REG(0x11);

	LCD_WR_REG(0x29);

}

void PY_2_4_LCD_init(void)
{
	LCD_WR_REG(0x36);
	LCD_WR_DATA8(0xa0);
	LCD_WR_REG(0x3A); 
	LCD_WR_DATA8(0x55);

	LCD_WR_REG(0xB0);
	LCD_WR_DATA8(0x00);
	LCD_WR_DATA8(0x08);

	LCD_WR_REG(0xB2);
	LCD_WR_DATA8(0x0C);
	LCD_WR_DATA8(0x0C);
	LCD_WR_DATA8(0x00);
	LCD_WR_DATA8(0x33);
	LCD_WR_DATA8(0x33); 

	LCD_WR_REG(0xB7); 
	LCD_WR_DATA8(0x35);  

	LCD_WR_REG(0xBB);
	LCD_WR_DATA8(0x28);

	LCD_WR_REG(0xC0);
	LCD_WR_DATA8(0x2C);

	LCD_WR_REG(0xC2);
	LCD_WR_DATA8(0x01);

	LCD_WR_REG(0xC3);
	LCD_WR_DATA8(0x0b);   

	LCD_WR_REG(0xC4);
	LCD_WR_DATA8(0x20);  

	LCD_WR_REG(0xC6); 
	LCD_WR_DATA8(0x0F);    

	LCD_WR_REG(0xD0); 
	LCD_WR_DATA8(0xA4);
	LCD_WR_DATA8(0xA1);

	LCD_WR_REG(0xE0);
	LCD_WR_DATA8(0xD0);
	LCD_WR_DATA8(0x01);
	LCD_WR_DATA8(0x08);
	LCD_WR_DATA8(0x0f);
	LCD_WR_DATA8(0x11);
	LCD_WR_DATA8(0x2a);
	LCD_WR_DATA8(0x36);
	LCD_WR_DATA8(0x55);
	LCD_WR_DATA8(0x44);
	LCD_WR_DATA8(0x3a);
	LCD_WR_DATA8(0x0b);
	LCD_WR_DATA8(0x06);
	LCD_WR_DATA8(0x11);
	LCD_WR_DATA8(0x20);

	LCD_WR_REG(0xE1);
	LCD_WR_DATA8(0xD0);
	LCD_WR_DATA8(0x02);
	LCD_WR_DATA8(0x07);
	LCD_WR_DATA8(0x0a);
	LCD_WR_DATA8(0x0b);
	LCD_WR_DATA8(0x18);
	LCD_WR_DATA8(0x34);
	LCD_WR_DATA8(0x43);
	LCD_WR_DATA8(0x4a);
	LCD_WR_DATA8(0x2b);
	LCD_WR_DATA8(0x1b);
	LCD_WR_DATA8(0x1c);
	LCD_WR_DATA8(0x22);
	LCD_WR_DATA8(0x1f);

	//LCD_WR_REG(0x21); 

	LCD_WR_REG(0x11);

	LCD_WR_REG(0x29);
}

/*st7789*/
void LCD_IO_Init(void)
{
	gpio_reset_pin(LCD_RES);
	gpio_reset_pin(LCD_BLK);
	gpio_set_direction(LCD_RES, GPIO_MODE_OUTPUT); // 设置io22为输出模式
	gpio_set_direction(LCD_BLK, GPIO_MODE_OUTPUT); // 设置io12为输出模式

	vspi_init();
	LCD_RES_CLR();
	DELAY(100);
	LCD_RES_SET();
	DELAY(100);
	BLK_ON();
	DELAY(100);

	LCD_WR_REG(0x11);
	DELAY(120);
#if ZJY_1_54
	ZJY_154_LCD_init();
#elif PY_2_4
	PY_2_4_LCD_init();
#endif
}
